//
// Created by LiuDongPeng on 2023/4/21.
//

#ifndef AS5600_AS5600_H
#define AS5600_AS5600_H


#include <stdint.h>
#include <stddef.h>


/* AS5600 device i2c address */
#define AS5600_I2C_ADDRESS 0x36


/**
 * @brief raw angle register
 */
#define RAW_ANGLE_MSB_REG 0x0C
#define RAW_ANGLE_LSB_REG 0x0D

/**
 * @brief AS5600
 */
typedef struct as5600
{
    /**
	 * @brief I2C memory write.
	 * @param[in]	dev_addr	    device i2c address
	 * @param[in]	mem_addr	    device memory address
     * @param[in]	mem_addr_size	device memory address size in byte, 1: 8bit, 2: 16bit
	 * @param[in]	buf			    data to write
	 * @param[in]	btw			    byte to write
     * @param[in]	timeout         operate timeout
	 * @return
	 */
    int (*i2c_mem_write_cb)(uint16_t dev_addr, uint16_t mem_addr, uint16_t mem_addr_size,
            uint8_t *buf, uint16_t btw, uint32_t timeout);

    /**
     * @brief I2C memory read.
     * @param[in]	dev_addr	    device i2c address
     * @param[in]	mem_addr	    device memory address
     * @param[in]	mem_addr_size	device memory address size in byte, 1: 8bit, 2: 16bit
     * @param[out]	buf			    data to read
     * @param[in]	btr			    byte to read
     * @param[in]	timeout         operate timeout
     * @return
     */
    int (*i2c_mem_read_cb)(uint16_t dev_addr, uint16_t mem_addr, uint16_t mem_addr_size,
            uint8_t *buf, uint16_t btr, uint32_t timeout);


    float lastAngle;
    float offset;

    float resolution;

} as5600_t;


typedef int (*i2c_mem_write_t)(uint16_t dev_addr, uint16_t mem_addr, uint16_t mem_addr_size,
        uint8_t *buf, uint16_t btw, uint32_t timeout);
typedef int (*i2c_mem_read_t)(uint16_t dev_addr, uint16_t mem_addr, uint16_t mem_addr_size,
        uint8_t *buf, uint16_t btr, uint32_t timeout);



int as5600_create(as5600_t* dev, i2c_mem_write_t i2c_mem_w_cb, i2c_mem_read_t i2c_mem_r_cb);

int16_t as5600_get_raw_angle(as5600_t* dev);

float as5600_get_angle_rad(as5600_t* dev);


int as5600_get_angle(as5600_t* dev, float* angle, float* rad);

#endif //AS5600_AS5600_H
